﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Data;
using Gollie.DataTables;
using MySQL.Db;

namespace Gollie.Ajax
{
	public class UserSaldi : AjaxResult
	{
		public Collection<UserSaldiInfo> SaldiInfo = new Collection<UserSaldiInfo>();

		public UserSaldi() : base(null) { }

		public UserSaldi(GetUserSaldiArg arg)
			: base(arg)
		{
		}

		protected override void Init(AjaxArg ajaxArg)
		{
			GetUserSaldiArg arg = (GetUserSaldiArg)ajaxArg;

			uint uid = Convert.ToUInt32(arg.tokenUser);

			if (uid == 1) //demo01
				this.GetDemoUserSaldi();
			else
			{
				ResultSet r = Database.Queries.GetUserSaldi(uid);

				for (int i = 0; i < r.Count; i++)
					SaldiInfo.Add(new UserSaldiInfo(uid, r.GetUInt32(i, "id")) { VendorId = r.GetUInt32(i, "id"), Name = r.GetString(i, "name"), City = r.GetString(i, "city"), Street = r.GetString(i, "street"), Saldo = r.GetDecimal(i, "saldo") });
			}
		}

		private void GetDemoUserSaldi()
		{
			Collection<Collection<UserSaldiMutation>> usm = new Collection<Collection<UserSaldiMutation>>() {
				new Collection<UserSaldiMutation>() 
				{
					new UserSaldiMutation() {  Amount = -2M, PurchaseDate = "16-02-2015, 21:47" },
					new UserSaldiMutation() {  Amount = -11.50M, PurchaseDate = "11-02-2015, 17:02" },
					new UserSaldiMutation() {  Amount = -22.25M, PurchaseDate = "31-01-2015, 01:13" },
					new UserSaldiMutation() {  Amount = 110M, PurchaseDate = "30-01-2015, 14:15" }
				},

				new Collection<UserSaldiMutation>() 
				{
					new UserSaldiMutation() {  Amount = -28M, PurchaseDate = "18-02-2015, 21:47" },
					new UserSaldiMutation() {  Amount = -87.50M, PurchaseDate = "01-02-2015, 17:02" },
					new UserSaldiMutation() {  Amount = -12M, PurchaseDate = "23-01-2015, 01:13" },
					new UserSaldiMutation() {  Amount = 250M, PurchaseDate = "22-01-2015, 19:20" }
				},

				new Collection<UserSaldiMutation>() 
				{
					new UserSaldiMutation() {  Amount = 75M, PurchaseDate = "09-02-2015, 09:20" }
				}
			};

			Collection<Collection<UserSaldiOffer>> uso = new Collection<Collection<UserSaldiOffer>>() {
				new Collection<UserSaldiOffer>() {
					new UserSaldiOffer() { Expires = "20-10-2015, 14:15", Saldo=usm[0].Sum(p=>p.Amount), Mutations = usm[0] }
				},
				new Collection<UserSaldiOffer>() {
					new UserSaldiOffer() { Expires = "12-10-2015, 19:20", Saldo=usm[1].Sum(p=>p.Amount), Mutations = usm[1] }
				},
				new Collection<UserSaldiOffer>() {
					new UserSaldiOffer() { Expires = "30-10-2015, 09:20", Saldo=usm[2].Sum(p=>p.Amount), Mutations = usm[2] }
				},
			};

			SaldiInfo.Add(new UserSaldiInfo() { VendorId = 1, Name = "Mijn Café", City = "Haarlem", Street = "Trollyweg", Saldo = uso[0][0].Saldo, SaldiDetailsOffers = uso[0] });
			SaldiInfo.Add(new UserSaldiInfo() { VendorId = 2, Name = "Restaurant Kok & Co", City = "Amsterdam", Street = "Kookweg 100", Saldo = uso[1][0].Saldo, SaldiDetailsOffers = uso[1] });
			SaldiInfo.Add(new UserSaldiInfo() { VendorId = 3, Name = "VV Sportkantine", City = "Haarlem", Street = "Hoofdweg 1", Saldo = uso[2][0].Saldo, SaldiDetailsOffers = uso[2] });
		}
	}

	public class UserSaldiInfo
	{
		public UserSaldiInfo() { } //demo

		public UserSaldiInfo(uint userId, uint vendorId)
		{
			UserActiveOfferdetailsPerVendor details = UserActiveOfferdetailsPerVendor.Instance(userId, vendorId);

			foreach (UserActiveOfferdetailsPerVendorItem item in details )
			{
				decimal saldo = item.Saldo;
				DateTime expires = item.ExpiratieDatum;
				uint mutationId = item.SaldoMutationId;
				decimal tegoed = item.Tegoed;
				DateTime startDate = item.Ingangsdatum;

				TransactionHistory transactions = details.Transactions[mutationId];

				SaldiDetailsOffers.Add(new UserSaldiOffer(transactions, tegoed, startDate) { Saldo = saldo, Expires = expires.ToString("dd-MM-yyyy, HH:mm") });
			}
		}

		public Collection<UserSaldiOffer> SaldiDetailsOffers = new Collection<UserSaldiOffer>();
		public uint VendorId;
		public string Name;
		public string Street;
		public string City;
		public decimal Saldo;
	}

	public class UserSaldiOffer
	{
		public UserSaldiOffer() { } //demo

		public UserSaldiOffer(TransactionHistory transactions, decimal tegoed, DateTime ingangsDatum)
		{
			foreach (TransactionHistoryItem item in transactions)
			{
				this.Mutations.Add(new UserSaldiMutation() { Amount = item.Bedrag, PurchaseDate = item.Datum.ToString("dd-MM-yyyy, HH:mm") });
			}

			this.Mutations.Add(new UserSaldiMutation() { Amount = tegoed, PurchaseDate = ingangsDatum.ToString("dd-MM-yyyy, HH:mm") });
		}

		public Collection<UserSaldiMutation> Mutations = new Collection<UserSaldiMutation>();
		public decimal Saldo;
		public string Expires;
	}

	public class UserSaldiMutation
	{
		public string PurchaseDate;
		public decimal Amount;
	}
}